﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "BurnDlgSle.h"
#include <QScrollBar>
#include <cfgmgr32.h>
#include <QFileDialog>
#include <QMessageBox>
#include "BurnToolCommon/Global.h"
#include "DataConvertor.h"
extern "C" {
#include <hidsdi.h>
};

namespace BurnDlg {

namespace  {
    const int MAX_STRING_WCHARS = 256;
    const int COLUMN_WIDTH_SIZE_20 = 20;
    const int COLUMN_WIDTH_SIZE_100 = 100;
    const int COLUMN_WIDTH_SIZE_200 = 200;
    const int COLUMN_MINIMUM_HEIGHT_SIZE_192 = 192;
    const int ADDRESS_SIZE = 6;
    const int FIELD_WIDTH_2 = 2;
}

BurnDlgSle::BurnDlgSle(QWidget *parent)
    : BurnDlg(parent),
      ui(new Ui::BurnDlgSle()),
      burnCtrlSle(new BurnCtrlSle()),
      sleThread(new MyThreadClass::MyThread()),
      actAbout(new QAction(stringClass.GetAbout())),
      aboutWin(nullptr),
      actChangeChip(new QAction(stringClass.GetChangeChip()))
{
    burnInfo.clear();
    qRegisterMetaType<WPARAM>("WPARAM");
    qRegisterMetaType<LPARAM>("LPARAM");
    qRegisterMetaType<long int>("long int");
    binModel = new BinFileModel();
    aboutWin = new AboutWindow();
    if (ui != nullptr && burnCtrlSle != nullptr && binModel != nullptr &&
        aboutWin != nullptr && sleThread != nullptr) {
        ui->setupUi(this);
        SlotCbnSetfocusComboCom();
        burnCtrlSle->moveToThread(sleThread);
        ui->menuHelp->addAction(actAbout);
        ui->progressBarReceive->setRange(0, 100); // 进度条范围0 ~ 100
        ui->progressBarReceive->setValue(0);
        ui->menuOption->addAction(actChangeChip);
        ui->menuSetting->addMenu(menuWifiLanguage);
        InitTableView();
        InitMenus();
        InitConnect();
        sleThread->start();
    }
}

BurnDlgSle::~BurnDlgSle()
{
    if (ui != nullptr) {
        delete ui;
        ui = nullptr;
    }

    if (burnCtrlSle != nullptr) {
        delete burnCtrlSle;
        burnCtrlSle = nullptr;
    }

    if (binModel != nullptr) {
        delete binModel;
        binModel = nullptr;
    }

    if (aboutWin != nullptr) {
        delete aboutWin;
        aboutWin = nullptr;
    }

    if (sleThread != nullptr) {
        delete sleThread;
        sleThread = nullptr;
    }
    if (changeChip != nullptr) {
        delete changeChip;
        changeChip = nullptr;
    }
}

void BurnDlgSle::InitConnect() const
{
    if (actChangeChip == nullptr || actAbout == nullptr || ui == nullptr ||
        burnCtrlSle == nullptr || sleThread == nullptr) {
        return;
    }
    connect(actChangeChip, &QAction::triggered, this, &BurnDlgSle::SlotShowChangeChip);
    // help
    connect(actAbout, &QAction::triggered, this, &BurnDlgSle::SlotShowAbout);
    // button
    connect(ui->btnConnect, &QPushButton::clicked, this, &BurnDlgSle::SlotBtnClickedConnect);
    connect(ui->btnSelectFile, &QPushButton::clicked, this, &BurnDlgSle::SlotBtnClickedSelectfile);
    connect(ui->btnStart, &QPushButton::clicked, this,  &BurnDlgSle::SlotBtnClickedStart);
    connect(ui->btnOpen, &QPushButton::clicked, this,  &BurnDlgSle::SlotBtnClickedOpen);
    // tableView
    connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked, this,
            &BurnDlgSle::SlotBnClickedTableHeader);
    connect(ui->tableView, &QTableView::clicked, this, &BurnDlgSle::SlotBnClickedTableView);
    connect(ui->tableView, &QTableView::doubleClicked, this, &BurnDlgSle::SlotBnDoubleClickedTableView);
    // burnctrl
    connect(burnCtrlSle, &BurnCtrlSle::SignalSetAddress, this, &BurnDlgSle::SlotSetAddress);
    connect(burnCtrlSle, &BurnCtrlSle::SignalOpenDeviceFail, this, &BurnDlgSle::SlotOpenDeviceFail);
    connect(burnCtrlSle, &BurnCtrlSle::SignalInsertPlainText, this, &BurnDlgSle::SlotInsertPlainText);
    connect(burnCtrlSle, &BurnCtrlSle::SignalConnectResult, this, &BurnDlgSle::SlotConnectResult);
    connect(burnCtrlSle, &BurnCtrlSle::SignalSetProgress, this, &BurnDlgSle::SlotSetProgress);
    // combo box
    connect(ui->comboBoxUsb, &ComboBoxCom::Clicked, this, &BurnDlgSle::SlotCbnSetfocusComboCom);
    // thread
    connect(sleThread, &QThread::finished, burnCtrlSle, &QObject::deleteLater);
    // burnCtrlSle
    connect(this, &BurnDlgSle::SignalSendMsg, burnCtrlSle, &BurnCtrlSle::SlotSendMsg);
}

void BurnDlgSle::SlotBtnClickedConnect()
{
    if (ui == nullptr) {
        return;
    }
    QString address = ui->cbAddress->currentText();
    QStringList addList = address.split(':');
    if (addList.size() > ADDRESS_SIZE) {
        return;
    }
    uint8_t *addr = new uint8_t[ADDRESS_SIZE]();
    if (addr == nullptr) {
        return;
    }
    for (int i = 0; i < addList.size(); i++) {
        bool ok = false;
        addr[i] = addList[i].toUShort(&ok, 0x10);
    }

    int j = 0;
    for (; j < infoDeviceInfo.size(); j++) {
        bool flag = true;
        for (int i = 0; i < ADDRESS_SIZE; i++) {
            if (infoDeviceInfo[j].addr[i] != addr[i]) {
                flag = false;
                continue;
            }
        }
        if (flag) {
            break;
        }
    }
    if (j == infoDeviceInfo.size()) {
        return;
    }
    if (ui->btnConnect->text() == stringClass.GetIdsString32918()) {
        emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_CONNECT),
                           reinterpret_cast<WPARAM>(&infoDeviceInfo[j]), sizeof(SleDeviceInfo));
        SlotInsertPlainText(stringClass.GetIdsString32897());
    } else if (ui->btnConnect->text() == stringClass.GetDisconnect()) {
        emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_DISCONNECT),
                           reinterpret_cast<WPARAM>(&infoDeviceInfo[j]), sizeof(SleDeviceInfo));
    }
}

void BurnDlgSle::SlotCbnSetfocusComboCom()
{
    if (ui == nullptr) {
        return;
    }
    ui->comboBoxUsb->clear();
    QStringList name;
    QStringList info;
    QString error;
    QStringList strListPortWifi;
    deviceInterfaceMap.clear();
    hidShowInfoMap.clear();
    deviceShowInfoMap.clear();
    HidEnumerate();
    QStringList hidName;
    for (auto it = deviceInterfaceMap.begin(); it != deviceInterfaceMap.end(); it++) {
        QString showStr = it.value().productName + " (vid: 0x" + QString::number(it.value().vid, 16) +
                          ", pid: 0x" + QString::number(it.value().pid, 16) +
                          ", usage: 0x" + QString::number(it.value().usage, 16) +
                          ", usage page: 0x" + QString::number(it.value().usagePage, 16) + ")";
        hidName.push_back(showStr);
        hidShowInfoMap[showStr] = it.value();
        deviceShowInfoMap[showStr] = it.key();
    }
    ui->comboBoxUsb->addItems(hidName);
}

void BurnDlgSle::SlotInsertPlainText(const QString &editStr)
{
    if (ui == nullptr) {
        return;
    }
    QScrollBar *scrollbar = ui->textEditReceive->verticalScrollBar();
    bool scrollbarAtButtom = (scrollbar->value() >= scrollbar->maximum() - 50);
    int scrollbarPrevValue = scrollbar->value();
    QTextCursor cursor = ui->textEditReceive->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->textEditReceive->setTextCursor(cursor);
    ui->textEditReceive->insertPlainText(editStr);
    if (scrollbarAtButtom) {
        ui->textEditReceive->ensureCursorVisible();
    } else {
        ui->textEditReceive->verticalScrollBar()->setValue(scrollbarPrevValue);
    }
}

void BurnDlgSle::SlotBtnClickedStart()
{
    if (burnCtrlSle == nullptr || ui == nullptr || binModel == nullptr) {
        return;
    }
    if (ui->btnStart->text() == stringClass.GetBinTextStop()) {
        ui->btnStart->setText(stringClass.GetBinTextStart());
        burnCtrlSle->SetIsStop();
        emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_STOP_BURN), 0, 0);
        return;
    }
    ui->btnStart->setText(stringClass.GetBinTextStop());
    UpdateList();
    burnInfo.clear();
    ui->progressBarReceive->setValue(0);
    QMap<int, BIN_INFO> binInfo = binModel->GetMapBinFileInfo();
    QMap<int, BIN_INFO> checkBinInfo;
    QMap<int, bool> binCheck = binModel->GetMapCheck();
    int i = 0;
    for (auto it = binInfo.begin(); it != binInfo.end(); it++) {
        if (binCheck[it.key()]) {
            checkBinInfo[i++] = it.value();
        }
    }
    if (checkBinInfo.size() == 0 || (!QFile(ui->lineEditSeleteFile->text()).exists())) {
        QMessageBox::warning(nullptr, MESSAGE_BURN_TOOL, stringClass.GetNoValidFlashFile());
        return;
    }
    burnCtrlSle->InitBurnCmdByArray(checkBinInfo);
    emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_START_BURN), 0, 0);
}

static QVector<SleDeviceInfo>::iterator GetSameAddrInInfoDeviceInfo(QVector<SleDeviceInfo> &infoDeviceInfo,
                                                                    uint8_t *addr,
                                                                    int size)
{
    if (size < ADDRESS_SIZE) {
        return infoDeviceInfo.end();
    }

    auto it = infoDeviceInfo.begin();
    for (; it != infoDeviceInfo.end(); it++) {
        bool flag = true;
        for (int i = 0; i < ADDRESS_SIZE; i++) {
            if (it->addr[i] != addr[i]) {
                flag = false;
                continue;
            }
        }
        if (flag) {
            break;
        }
    }

    return it;
}

void BurnDlgSle::SlotBtnClickedOpen()
{
    if (ui == nullptr || burnCtrlSle == nullptr) {
        return;
    }
    if (ui->btnOpen->text() == stringClass.GetBinTextOpen()) {
        ui->btnOpen->setText(stringClass.GetBinTextClose());
        burnCtrlSle->SetDeviceInterface(deviceShowInfoMap[ui->comboBoxUsb->currentText()]);
        emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_OPEN), 0, 0);
        SlotInsertPlainText(tr("Opening... \n"));
    } else {
        ui->btnOpen->setText(stringClass.GetBinTextOpen());
        if (ui->btnConnect->text() == stringClass.GetDisconnect()) {
            QString address = ui->cbAddress->currentText();
            QStringList addList = address.split(':');
            if (addList.size() > ADDRESS_SIZE) {
                return;
            }
            uint8_t *addr = new uint8_t[ADDRESS_SIZE]();
            if (addr == nullptr) {
                return;
            }
            for (int i = 0; i < addList.size(); i++) {
                bool ok = false;
                addr[i] = addList[i].toUShort(&ok, 0x16);
            }
            auto it = GetSameAddrInInfoDeviceInfo(infoDeviceInfo, addr, ADDRESS_SIZE);
            emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_DISCONNECT),
                               reinterpret_cast<WPARAM>(it),
                               sizeof(SleDeviceInfo));
        }
        Sleep(500); // 断连后延时500ms关闭sle
        emit SignalSendMsg(static_cast<long int>(SleThreadMsg::SLE_THREAD_MSG_CLOSE), 0, 0);
        SlotInsertPlainText(tr("Close! \n"));
    }
}

void BurnDlgSle::UpdateList()
{
    if (binModel == nullptr) {
        return;
    }
    int row = binModel->rowCount();
    if (row > 0x1000) {
        return;
    }
    std::vector<QString> pathArray;
    for (int i = 0; i < row; i++) {
        if (binModel->GetCheck(i)) {
            QString path = binModel->GetValue(i, TABLEVIEW_COLUMN_PAHT);
            pathArray.push_back(path);
        }
    }
    std::vector<QString> result;
    for (auto item : pathArray) {
        bool needAddDlg = true;
        for (auto item2 : result) {
            if (item2 == item) {
                needAddDlg = false;
                break;
            }
        }
        if (needAddDlg) {
            result.push_back(item);
        }
    }
    QString version = "";
    for (auto item : result) {
        BIN_INFO_ARRAY infoArray;
        DataConvertor::AnalysMultifBin(item, infoArray, version, isEncrypt, encryptBuffer);
        UpdateList(infoArray);
    }
}

void BurnDlgSle::UpdateList(BIN_INFO_ARRAY &info) const
{
    if (binModel == nullptr) {
        return;
    }
    for (int i = 0; i < info.GetSize(); i++) {
        BIN_INFO item = info.GetAt(i);
        int colNum = binModel->rowCount(); // 取行数
        if (colNum > 0x1000) {
            break;
        }
        QMap<int, BIN_INFO> mapBinInfo;
        mapBinInfo.clear();
        mapBinInfo = binModel->GetMapBinFileInfo();
        for (int j = 0; j < colNum; j++) {
            if ((!item.fileName.isEmpty()) && binModel->GetCheck(j) &&
                item.path == mapBinInfo[j].path && item.fileName == mapBinInfo[j].fileName &&
                item.type == mapBinInfo[j].type && (mapBinInfo[j].fileIndex != item.fileIndex ||
                mapBinInfo[j].fileSize != item.fileSize || mapBinInfo[j].burnAddr != item.burnAddr ||
                mapBinInfo[j].burnSize != item.burnSize)) {
                mapBinInfo[j].fileIndex = item.fileIndex;
                mapBinInfo[j].fileSize = item.fileSize;
                mapBinInfo[j].burnAddr = item.burnAddr;
                mapBinInfo[j].burnSize = item.burnSize;
            } else if (item.fileName.isEmpty() && binModel->GetCheck(j) && item.path == mapBinInfo[j].path &&
                       item.fileName == mapBinInfo[j].fileName && item.fileSize != mapBinInfo[j].fileSize) {
                mapBinInfo[j].fileSize = item.fileSize;
            }
        }
        binModel->FetchData(mapBinInfo);
    }
}

void BurnDlgSle::SlotBtnClickedSelectfile()
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("Select File"), "", stringClass.GetIdsString32941());
    if (!filePath.isEmpty()) {
        AnalyseBinFile(filePath);
    }
}

void BurnDlgSle::AnalyseBinFile(QString filePath)
{
    if (burnCtrlSle == nullptr || burnCtrlSle->IsBusy()) {
        QMessageBox::critical(this, stringClass.GetMessageError(),
                              stringClass.GetIdsString32984(), stringClass.GetYes());
        return;
    }
    if (ui == nullptr || binModel == nullptr) {
        return;
    }
    ui->lineEditSeleteFile->setText(filePath);
    QMap<int, bool> mapLastBinCheck = binModel->GetMapCheck();
    QMap<int, BIN_INFO> mapLastBinInfo = binModel->GetMapBinFileInfo();
    binModel->ClearData();
    BIN_INFO_ARRAY infoArray;
    QString version;
    QMap<int, BIN_INFO> mapBinInfo = binModel->GetMapBinFileInfo();
    DataConvertor::AnalysMultifBin(ui->lineEditSeleteFile->text(), infoArray, version,
                                   isEncrypt, encryptBuffer);
    for (int i = 0; i < infoArray.GetSize(); i++) {
        if (infoArray.GetAt(i).type == IMAGE_TYPE::IMAGE_TYPE_SMALL_SSB ||
            infoArray.GetAt(i).type == IMAGE_TYPE::IMAGE_TYPE_SMALL_SSB_SHA) {
            continue;
        }
        mapBinInfo[mapBinInfo.size()] = infoArray.GetAt(i);
    }
    QMap<int, bool> mapCheck;
    for (int i = 0; i < mapBinInfo.size(); i++) {
        mapCheck[i] = true;
    }
    binModel->FetchData(mapCheck);
    binModel->FetchData(mapBinInfo);
}

void BurnDlgSle::InitTableView() const
{
    if (ui == nullptr || binModel == nullptr) {
        return;
    }
    ui->tableView->setModel(binModel);
    ui->tableView->setDragEnabled(true);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setDefaultDropAction(Qt::MoveAction);
    ui->tableView->setDragDropMode(QAbstractItemView::InternalMove);
    ui->tableView->setColumnWidth(TABLEVIEW_COLUMN_CHECK, COLUMN_WIDTH_SIZE_20);
    ui->tableView->setColumnWidth(TABLEVIEW_COLUMN_ADD, COLUMN_WIDTH_SIZE_20);
    ui->tableView->setColumnWidth(TABLEVIEW_COLUMN_DELETE, COLUMN_WIDTH_SIZE_20);
    ui->tableView->setColumnWidth(TABLEVIEW_COLUMN_NAME, COLUMN_WIDTH_SIZE_100);
    ui->tableView->setColumnWidth(TABLEVIEW_COLUMN_PAHT, COLUMN_WIDTH_SIZE_200);
    ui->tableView->setMinimumHeight(COLUMN_MINIMUM_HEIGHT_SIZE_192);
    ui->tableView->horizontalHeader()->setSectionResizeMode(TABLEVIEW_COLUMN_CHECK, QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setSectionResizeMode(TABLEVIEW_COLUMN_ADD, QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setSectionResizeMode(TABLEVIEW_COLUMN_DELETE, QHeaderView::Fixed);
}

void BurnDlgSle::InitMenus()
{
    this->tableViewMenu = new QMenu(this);
    QAction *createNewFileAction = new QAction(stringClass.GetCreateNewFile());
    if (createNewFileAction != nullptr) {
        this->tableViewMenu->addAction(createNewFileAction);
        connect(createNewFileAction, &QAction::triggered, this, &BurnDlgSle::SlotCreateNewFwpkgFile);
    }
    QAction *saveSpiltFileAction = new QAction(stringClass.GetSaveSplitFile());
    if (saveSpiltFileAction != nullptr) {
        this->tableViewMenu->addAction(saveSpiltFileAction);
        connect(saveSpiltFileAction, &QAction::triggered, this, &BurnDlgSle::SlotSaveSpiltFwpkgFile);
    }
}

void BurnDlgSle::contextMenuEvent(QContextMenuEvent *event)
{
    Q_UNUSED(event);
    if (!ui->tableView->selectionModel()->currentIndex().isValid()) {
        return;
    }
    this->tableViewMenu->exec(QCursor::pos());
}

void BurnDlgSle::SlotCreateNewFwpkgFile()
{
    CreateNewFwpkgFile(ui->lineEditSeleteFile->text());
}

void BurnDlgSle::SlotSaveSpiltFwpkgFile()
{
    SaveSpiltFwpkgFile(ui->lineEditSeleteFile->text());
}

void BurnDlgSle::HidEnumerate()
{
    GUID interfaceClassGuid;
    CONFIGRET cr;
    CHAR* deviceInterfaceList = nullptr;
    DWORD len = 0;
    HidD_GetHidGuid(&interfaceClassGuid);
    do {
        cr = CM_Get_Device_Interface_List_SizeA(&len, &interfaceClassGuid,
                                                nullptr, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
        if (cr != CR_SUCCESS) {
            BurnDlgSle::SlotInsertPlainText(tr("Failed to get size of HID device interface list. \n"));
            break;
        }

        if (deviceInterfaceList != nullptr) {
            delete [] deviceInterfaceList;
            deviceInterfaceList = nullptr;
        }

        if (len <= 0) {
            printf("Memory is not compliant");
        }
        deviceInterfaceList = new CHAR[len]();
        if (deviceInterfaceList == nullptr) {
            BurnDlgSle::SlotInsertPlainText(tr("Failed to allocate memory for HID device interface list. \n"));
            return;
        }
        cr = CM_Get_Device_Interface_ListA(&interfaceClassGuid, nullptr, deviceInterfaceList,
                                           len, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
        if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL) {
            BurnDlgSle::SlotInsertPlainText(tr("Failed to get HID device interface list. \n"));
        }
    } while (cr == CR_BUFFER_SMALL);
    if (deviceInterfaceList == nullptr) {
        return;
    }
    if (cr != CR_SUCCESS) {
        delete [] deviceInterfaceList;
        deviceInterfaceList = nullptr;
        return;
    }
    HidEnumerateInterface(deviceInterfaceList, len);
}

void BurnDlgSle::HidEnumerateInterface(CHAR* deviceInterfaceList, DWORD len)
{
    Q_UNUSED(len);
    for (CHAR* deviceDlgInterface = deviceInterfaceList; (*deviceDlgInterface) != 0;
         deviceDlgInterface += strlen(deviceDlgInterface) + 1) {
        HANDLE deviceHandle = INVALID_HANDLE_VALUE;
        HidDeviceInfo hidDeviceInfo;
        /* Open read-only handle to the device */
        deviceHandle = OpenDevice(deviceDlgInterface, FALSE);
        /* Check validity of device_handle. */
        if (deviceHandle == INVALID_HANDLE_VALUE) {
            /* Unable to open the device. */
            continue;
        }
        wchar_t string[MAX_STRING_WCHARS] = {0};
        QString productString = "";
        if (HidD_GetProductString(deviceHandle, string, MAX_STRING_WCHARS)) {
            string[MAX_STRING_WCHARS - 1] = 0;
            productString = QString::fromWCharArray(string, wcslen(string));
            hidDeviceInfo.productName = productString;
        }
        HIDD_ATTRIBUTES attrib;

        if (HidD_GetAttributes(deviceHandle, &attrib)) {
            hidDeviceInfo.vid = attrib.VendorID;
            hidDeviceInfo.pid = attrib.ProductID;
        }
        PHIDP_PREPARSED_DATA data = nullptr;
        HIDP_CAPS caps;
        if (HidD_GetPreparsedData(deviceHandle, &data)) {
            if (HidP_GetCaps(data, &caps) == HIDP_STATUS_SUCCESS) {
                hidDeviceInfo.usagePage = caps.UsagePage;
                hidDeviceInfo.usage = caps.Usage;
            }
            deviceInterfaceMap[QString::fromUtf8(deviceDlgInterface)] = hidDeviceInfo;
            HidD_FreePreparsedData(data);
        }
        CloseHandle(deviceHandle);
    }
}

HANDLE BurnDlgSle::OpenDevice(const CHAR *path, BOOL openRw) const
{
    DWORD desiredAccess = (openRw) ? (GENERIC_WRITE | GENERIC_READ) : 0;
    DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
    HANDLE handle = CreateFileA(path, desiredAccess, shareMode, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
    return handle;
}
void BurnDlgSle::SlotSetAddress(const QVector<SleDeviceInfo>& info)
{
    if (ui == nullptr) {
        return;
    }
    ui->cbAddress->clear();
    QString address = "";
    QStringList addressList;
    for (auto it = info.begin(); it != info.end(); it++) {
        address.clear();
        for (int i = 0; i < ADDRESS_SIZE; i++) {
            if (i + 1 == ADDRESS_SIZE) {
                address += QString("%1").arg(it->addr[i], FIELD_WIDTH_2, 0x10, QChar('0'));
                continue;
            }
            address += QString("%1").arg(it->addr[i], FIELD_WIDTH_2, 0x10, QChar('0')) + ":";
        }
        addressList.push_back(address);
    }
    infoDeviceInfo = info;
    ui->cbAddress->addItems(addressList);
}

void BurnDlgSle::SlotOpenDeviceFail() const
{
    if (ui == nullptr) {
        return;
    }
    ui->btnOpen->setText(stringClass.GetBinTextOpen());
}

void BurnDlgSle::SlotConnectResult(bool result) const
{
    if (ui == nullptr) {
        return;
    }
    if (result) {
        ui->btnConnect->setText(stringClass.GetDisconnect());
    } else {
        ui->btnConnect->setText(stringClass.GetIdsString32918());
    }
}

void BurnDlgSle::SlotSetProgress(double progress) const
{
    if (ui == nullptr) {
        return;
    }
    ui->progressBarReceive->setValue(progress);
}

}
